// $Id: AtomView.java 376 2009-05-26 09:39:39Z huijzer $

/*    
    Aoide is a web-based music catalog using the Spring Framework.
    Copyright (C) 2009  Arjan Huijzer

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

    You can contact Arjan Huijzer by sending an e-mail to huijzer@gmail.com.
*/


package org.aoide.web.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aoide.core.persistence.Recording;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.view.feed.AbstractAtomFeedView;

import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
import com.sun.syndication.feed.atom.Link;

public class AtomView extends AbstractAtomFeedView {

	@Override
	protected void buildFeedMetadata(Map<String, Object> model, Feed feed,
			HttpServletRequest request) {
 
		String id = (String) model.get("id");
		String title = (String) model.get("title");
		
		if (StringUtils.isNotBlank(id)) {
			feed.setId(id);	
		}
		
		feed.setTitle(title);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected List<Entry> buildFeedEntries(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Entry> entries = new ArrayList<Entry>();
		List<Recording> recordings = (List<Recording>) model.get("recordings");
		for (Recording recording : recordings) {
			Entry entry = new Entry();
			entry.setId(recording.getUuid());
			entry.setTitle(recording.getTitle());
			
			List<Link> alternateLinks = new ArrayList<Link>();
			Link link = new Link();
			link.setHref(String.format("http://aoide.appspot.com/recordingDisplay.action?uuid=%s", recording.getUuid()));
			alternateLinks.add(link);
			entry.setAlternateLinks(alternateLinks);
			
			entries.add(entry);
		}
		return entries;
	}


}
